//package com.plian.system.service.im.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.DateUtil;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.common.status.CheckStatus;
//import com.plian.system.config.YthTaskObserver;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.constant.yth.YthConstant;
//import com.plian.system.dao.sys.file.FileDao;
//import com.plian.system.entity.im.InvestmentEvaluation;
//import com.plian.system.entity.im.InvestmentEvaluationDetail;
//import com.plian.system.entity.im.ProjectInfo;
//import com.plian.system.mapper.im.InvestmentEvaluationMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ListResult;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.search.SearchParamEntiy;
//import com.plian.system.mybatis.search.TokenSearch;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.mybatis.support.Query;
//import com.plian.system.service.im.IInvestmentEvaluationDetailService;
//import com.plian.system.service.im.IInvestmentEvaluationService;
//import com.plian.system.service.im.IProjectInfoService;
//import com.plian.system.service.sys.file.FileService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.sys.user.ISysUserService;
//import com.plian.system.service.yth.IProjectEvaluateService;
//import com.plian.system.vo.im.InvestmentEvaluationVO;
//import com.plian.system.vo.yth.YthResult;
//import com.plian.system.wrapper.im.InvestmentEvaluationWrapper;
//import lombok.AllArgsConstructor;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @Author: Take-off
// * @Description: TODO
// * @Date: 2023/2/8 10:54 AM
// * @Version: 1.0
// **/
//@Service
//@AllArgsConstructor
//public class InvestmentEvaluationServiceImpl extends BaseServiceImpl<InvestmentEvaluationMapper,InvestmentEvaluation> implements IInvestmentEvaluationService {
//
//    private final IInvestmentEvaluationDetailService evaluationDetailService;
//
//    private final InvestmentEvaluationWrapper investmentEvaluationWrapper;
//
//    private FileDao fileDao;
//
//    private FileService fileService;
//
//    private final IOrgService orgService;
//
//    private final ISysUserService userService;
//
//    private final IProjectInfoService projectInfoService;
//
//    private final IProjectEvaluateService projectEvaluateService;
//
//    @Override
//    public boolean saveOrUpdate(InvestmentEvaluation investmentEvaluation) {
//        if (!Optional.ofNullable(investmentEvaluation.getId()).isPresent()) {
//            investmentEvaluation.setFormType(String.valueOf(FormTypeConstant.IM_BASE_INVESTMENT_EVALUATION));
//        }
//        return super.saveOrUpdate(investmentEvaluation);
//    }
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            InvestmentEvaluation investmentEvaluation = getById(id);
//            if (investmentEvaluation == null) {
//                return false;
//            }
//        }
//        boolean result = super.deleteLogic(ids);
//        if (result){
//            evaluationDetailService.deleteByParentId(ids);
//        }
//        return result;
//    }
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean submit(InvestmentEvaluation investmentEvaluation) {
//        boolean result = saveOrUpdate(investmentEvaluation);
//        if (result){
//            List<InvestmentEvaluationDetail> evaluationDetails = investmentEvaluation.getEvaluationDetails();
//            if (CollectionUtil.isNotEmpty(evaluationDetails)){
//                for (InvestmentEvaluationDetail evaluationDetail : evaluationDetails){
//                    if (evaluationDetail.getId() != null &&
//                            Optional.ofNullable(evaluationDetail.getIsDeleted()).isPresent() && evaluationDetail.getIsDeleted().intValue() == 1) {
//                        evaluationDetailService.removeById(evaluationDetail.getId());
//                    }else {
//                        evaluationDetail.setEvaluationId(investmentEvaluation.getId());
//                        evaluationDetailService.saveOrUpdate(evaluationDetail);
//                    }
//                }
//            }
//        }
//        return result;
//    }
//
//    @Override
//    public InvestmentEvaluationVO getDetailObj(InvestmentEvaluation investmentEvaluation) {
//        InvestmentEvaluation detail = getOne(Condition.getQueryWrapper(investmentEvaluation));
//        InvestmentEvaluationVO investmentEvaluationVO = investmentEvaluationWrapper.entityToVO(detail);
//
//        //子项
//        List<InvestmentEvaluationDetail> details = evaluationDetailService.list(new LambdaQueryWrapper<InvestmentEvaluationDetail>().eq(InvestmentEvaluationDetail::getEvaluationId, investmentEvaluationVO.getId()));
//
//        if (CollectionUtil.isNotEmpty(details)){
//            List<String> ids = details.stream().map(m -> m.getId() + "").collect(Collectors.toList());
//            try {
//                List<PageData> files = fileDao.findbyParentIds(ids);
//                Map<String, List<PageData>> childMap = files.stream().collect(Collectors.groupingBy(file -> file.getString("parentId")));
//                details.forEach(evaluationDetail ->{
//                    List<HashMap> hashMaps = new ArrayList<>();
//                    List<PageData> file = childMap.get(evaluationDetail.getId()+"");
//                    if (CollectionUtil.isNotEmpty(file)){
//                        file.forEach(m ->{
//                            fileService.setViewInfo(m);
//                            hashMaps.add(m);
//                        });
//                    }
//                    evaluationDetail.setDocList(hashMaps);
//
//                });
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            investmentEvaluationVO.setEvaluationDetails(details);
//        }
//        return investmentEvaluationVO;
//    }
//
//    @Override
//    public ListResult<InvestmentEvaluationVO> list(Query query) {
//        List<SearchEntity> entityList = query.getSearch();
//        if (query.getAscs()==null&&query.getDescs()==null){
//            query.setDescs("create_time");
//        }
//        QueryWrapper<InvestmentEvaluation> queryWrapper = SearchParamEntiy.getQueryWrapper(
//                Condition.getQueryWrapper(new InvestmentEvaluation()), entityList
//        );
//        TokenSearch<InvestmentEvaluation> tokenSearch = new TokenSearch<>();
//        tokenSearch.addTokenNoFlowQuery(queryWrapper, query);
//        IPage<InvestmentEvaluation> pages = page(Condition.getPage(query), queryWrapper);
//        List<InvestmentEvaluationVO> voList = investmentEvaluationWrapper.entityToVO(pages.getRecords());
//        ListResult<InvestmentEvaluationVO> listResult = new ListResult<>(CommonCode.SUCCESS);
//        listResult.setList(voList);
//        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//        return listResult;
//    }
//
//    @Override
//    @Transactional(rollbackFor = RuntimeException.class)
//    public String saveAnnualInvestmentEvaluation(String data){
//        YthResult ythResult = new YthResult();
//        //成功的id集合
//        List<String> successIds = new ArrayList<>();
//        //失败的id集合
//        List<String> failIds = new ArrayList<>();
//        //失败的原因
//        List<String> failReasons = new ArrayList<>();
//        //将json字符串转换成list
//        List<InvestmentEvaluation> dataList = JSON.parseArray(data, InvestmentEvaluation.class);
//        if (CollectionUtil.isNotEmpty(dataList)){
//            //根据datasourceIds 查询出所有的数据
//            List<InvestmentEvaluation> list = list(new LambdaQueryWrapper<InvestmentEvaluation>().in(InvestmentEvaluation::getDataSourceId, dataList.stream().map(InvestmentEvaluation::getDataSourceId).toArray()));
//            if (CollectionUtil.isNotEmpty(list)){
//                //删除子集
//                List<Long> ids = list.stream().map(InvestmentEvaluation::getId).collect(Collectors.toList());
//                evaluationDetailService.remove(new LambdaQueryWrapper<InvestmentEvaluationDetail>().in(InvestmentEvaluationDetail::getEvaluationId, ids));
//            }
//            //如果数据库中有数据，就更新，没有就新增
//            for (InvestmentEvaluation entity:dataList){
//                //id重复略过
//                if (CollectionUtil.isNotEmpty(list)){
//                    List<String> olIds = list.stream().map(InvestmentEvaluation::getDataSourceId).collect(Collectors.toList());
//                    String dataSourceId = entity.getDataSourceId();
//                    //判断数据库中是否有数据
//                    if (olIds.contains(dataSourceId)){
//                        for (InvestmentEvaluation oldEntity:list){
//                            if (dataSourceId.equals(oldEntity.getDataSourceId())){
//                                //判断status是否为0，如果是0，就更新，不是0，就不更新
//                                if (CheckStatus.checkCode(oldEntity.getStatus())){
//                                    successIds.add(dataSourceId);
//                                    entity.setId(oldEntity.getId());
//                                    entity.setDataSource(YthConstant.DATA_NINGBO);
//                                    submit(entity);
//                                    list.remove(oldEntity);
//                                    break;
//                                }else {
//                                    failIds.add(dataSourceId);
//                                    failReasons.add("dataSourceId:"+dataSourceId+"数据已提交且已经在流程中，无法更新");
//                                    break;
//                                }
//                            }
//                        }
//                    }else {
//                        //如果数据库中没有数据，就新增
//                        entity.setDataSource(YthConstant.DATA_NINGBO);
//                        submit(entity);
//                        successIds.add(entity.getDataSourceId());
//                    }
//                }else {
//                    //如果数据库中没有数据，就新增
//                    entity.setDataSource(YthConstant.DATA_NINGBO);
//                    submit(entity);
//                    successIds.add(entity.getDataSourceId());
//                }
//            }
//        } else {
//            failReasons.add("数据为空");
//        }
//        ythResult.setSuccessIds(successIds);
//        ythResult.setFailIds(failIds);
//        ythResult.setFailReason(StringUtils.join(failReasons, ";"));
//        //ythResult转换成json字符串
//        return JSON.toJSONString(ythResult);
//    }
//
//    @Override
//    @YthTaskObserver
//    public boolean saveInvestmentEvaluation(String date){
//        boolean flag = true;
//        Date start = DateUtil.parse(date, DateUtil.PATTERN_DATE);
//        //获取数据
////        List<InvestmentEvaluation> investmentEvaluations = projectEvaluateService.getAllProject(start, DateUtil.plusDays(start, 1));
//        List<InvestmentEvaluation> investmentEvaluations = projectEvaluateService.getAllProject(null,null);
//        remove(Wrappers.<InvestmentEvaluation>lambdaQuery().eq(InvestmentEvaluation::getDataSource,3));
//
//        Map<String, String> orgSourceIds = orgService.getSourceId();
//        List<ProjectInfo> projectInfos = projectInfoService.list(new LambdaQueryWrapper<ProjectInfo>().eq(ProjectInfo::getDataSource, 3)
//                .isNotNull(ProjectInfo::getDataSourceId));
//        for (InvestmentEvaluation investmentEvaluation:investmentEvaluations){
//            List<InvestmentEvaluationDetail> evaluationDetails = investmentEvaluation.getEvaluationDetails();
//            String orgId = orgSourceIds.get(investmentEvaluation.getReportOrgId());
//            if (StringUtils.isNotBlank(orgId)){
//                investmentEvaluation.setReportOrgId(orgId);
//                investmentEvaluation.setCreateCompanyId(orgId);
//            }
//            for (InvestmentEvaluationDetail detail:evaluationDetails){
//                Optional<ProjectInfo> projectInfo = projectInfos.stream().filter(m -> detail.getProjectInfoId().toString().equals(m.getDataSourceId())).findFirst();
//                if (projectInfo.isPresent()){
//                    ProjectInfo info = projectInfo.get();
//                    detail.setProjectInfoId(info.getId());
//                    detail.setProjectInfoName(info.getProjectName());
//                }
//            }
//            investmentEvaluation.setStatus(FormStatusConstant.FINISH);
//            flag = submit(investmentEvaluation);
//        }
//        return flag;
//    }
//
//}
